package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult register(User user){
        JSONResult result = new JSONResult();
        try {
            userService.save(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public JSONResult update(User user){
        JSONResult result = new JSONResult();
          userService.updat(user);
        return result.setResult(user);
    }
    @GetMapping("/{authorId}/travels")
    public PageInfo listTravels(TravelQuery qo){
        //按照最后更新时间倒序
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }
}
